from selenium.webdriver.common.by import By

from base.page_base import PageBase


class AddressPage(PageBase):
    """地址管理页面"""
    # 元素定位
    # 1.个人
    __mine = (By.XPATH, '//*[@text="个人"]')
    # 2.设置
    __setting = (By.ID, "com.netease.yanxuan:id/ivSetting")
    # 3.我的地址
    __address = (By.ID, 'com.netease.yanxuan:id/address_manager')
    # 4.新建地址
    __add_address = (By.XPATH, '//android.widget.TextView[@text="新建地址"]')
    # 5.选择省份,市,街道
    __province = (By.XPATH, '//android.widget.TextView[@text="北京市"]')
    __city = (By.XPATH, '//android.widget.TextView[@text="顺义区"]')
    __county = (By.XPATH, '//android.widget.TextView[@text="马坡地区"]')
    # 6.确定
    __confirm = (By.ID, 'com.netease.yanxuan:id/btn_confirm_address_manage')
    # 7.详细地址
    __detail_address = (By.ID, 'com.netease.yanxuan:id/address_detial_edit')
    # 8.姓名
    __name = (By.ID, 'com.netease.yanxuan:id/address_name_edit')
    # 9.手机号
    __phone = (By.ID, 'com.netease.yanxuan:id/address_phonenumber_edit')
    # 10.保存
    __save = (By.ID, 'com.netease.yanxuan:id/nav_right_text')

    # 11.获取添加成功文本
    __success_text = (By.XPATH, '//android.widget.TextView[@text="张三"]')

    # 操作方法
    def click_mine(self):
        """点击个人"""
        self.base_click(self.__mine)

    def clcik_setting(self):
        """点击设置"""
        self.base_click(self.__setting)

    def click_my_address(self):
        """点击我的地址"""
        self.base_click(self.__address)

    def select_province(self):
        """选择省份"""
        self.base_click(self.__add_address)
        self.base_click(self.__province)

    def select_city(self, x=500, start_y=1800, end_y=500):
        """选择城市"""
        self.base_swipe(x, start_y, x, end_y)
        self.base_click(self.__city)

    def select_county(self, x=500, start_y=1800, end_y=1700):
        """选择街道"""
        self.base_swipe(x, start_y, x, end_y)
        self.base_click(self.__county)
        self.base_click(self.__confirm)

    def input_detail_address(self, detail_address, name, phone):
        """输入详细地址"""
        self.base_input_text(self.__detail_address, detail_address)
        self.base_input_text(self.__name, name)
        self.base_input_text(self.__phone, phone)
        self.base_click(self.__save)

    # 添加地址业务流程
    def add_shipping_address(self, detail_address, name, phone):
        """业务流程"""
        self.click_mine()
        self.clcik_setting()
        self.click_my_address()
        self.select_province()
        self.select_city()
        self.select_county()
        self.input_detail_address(detail_address, name, phone)

    def get_success_text(self):
        """获取成功文本"""
        return self.base_get_text(self.__success_text)
